﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Security
{
    public class XXTea
    {
        private static volatile XXTea instance;

        private static object syncRoot = new Object();

        public static XXTea Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new XXTea();
                    }
                }

                return instance;

            }
        }

        public byte[] Encrypt(byte[] data, byte[] key)
        {
            if (data.Length == 0)
            {
                return data;
            }
            return ToByteArray(Encrypt(ToUInt32Array(data, true), ToUInt32Array(key, false)), false);
        }

        public byte[] Decrypt(byte[] data, byte[] key)
        {
            if (data.Length == 0)
            {
                return data;
            }
            return ToByteArray(Decrypt(ToUInt32Array(data, false), ToUInt32Array(key, false)), true);
        }

        public UInt32[] Encrypt(UInt32[] v, UInt32[] k)
        {
            Int32 n = v.Length - 1;
            if (n < 1)
            {
                return v;
            }
            if (k.Length < 4)
            {
                UInt32[] key = new UInt32[4];
                k.CopyTo(key, 0);
                k = key;
            }
            UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum = 0, e;
            Int32 p, q = 6 + 52 / (n + 1);
            while (q-- > 0)
            {
                sum = unchecked(sum + delta);
                e = sum >> 2 & 3;
                for (p = 0; p < n; p++)
                {
                    y = v[p + 1];
                    z = unchecked(v[p] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
                }
                y = v[0];
                z = unchecked(v[n] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
            }
            return v;
        }

        public UInt32[] Decrypt(UInt32[] v, UInt32[] k)
        {
            Int32 n = v.Length - 1;
            if (n < 1)
            {
                return v;
            }
            if (k.Length < 4)
            {
                UInt32[] key = new UInt32[4];
                k.CopyTo(key, 0);
                k = key;
            }
            UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum, e;
            Int32 p, q = 6 + 52 / (n + 1);
            sum = unchecked((UInt32)(q * delta));
            while (sum != 0)
            {
                e = sum >> 2 & 3;
                for (p = n; p > 0; p--)
                {
                    z = v[p - 1];
                    y = unchecked(v[p] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
                }
                z = v[n];
                y = unchecked(v[0] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
                sum = unchecked(sum - delta);
            }
            return v;
        }

        private UInt32[] ToUInt32Array(byte[] data, Boolean includeLength)
        {
            Int32 n = (((data.Length & 3) == 0) ? (data.Length >> 2) : ((data.Length >> 2) + 1));
            UInt32[] Result;
            if (includeLength)
            {
                Result = new UInt32[n + 1];
                Result[n] = (UInt32)data.Length;
            }
            else
            {
                Result = new UInt32[n];
            }
            n = data.Length;
            for (Int32 i = 0; i < n; i++)
            {
                Result[i >> 2] |= (UInt32)data[i] << ((i & 3) << 3);
            }
            return Result;
        }

        private byte[] ToByteArray(UInt32[] data, Boolean includeLength)
        {
            Int32 n;
            if (includeLength)
            {
                n = (Int32)data[data.Length - 1];
            }
            else
            {
                n = data.Length << 2;
            }
            byte[] Result = new byte[n];
            for (Int32 i = 0; i < n; i++)
            {
                Result[i] = (byte)(data[i >> 2] >> ((i & 3) << 3));
            }
            return Result;
        }


        private void test()
        {
            string demo = "helloworld2";

            string key = "fuckyousfuckyous";

            byte[] encrypt =
                Encrypt(
                Pixysoft.StringCoder.StringToByte(demo),
                Pixysoft.StringCoder.StringToByte(key));

            byte[] decrypt = Decrypt(encrypt,
                Pixysoft.StringCoder.StringToByte(key));

            Console.WriteLine(Pixysoft.StringCoder.ByteToString(encrypt));

            Console.WriteLine(Pixysoft.StringCoder.ByteToString(decrypt));
        }
    }
}
